package spaska.data;

import java.util.Arrays;
import java.util.Iterator;

public class Instance implements Cloneable, Iterable<Value> {

	private Value[] vector;
	private double weight;
	private int classIndex;

	public Instance() {
		vector = new Value[0];
	}

	public Instance(Value... vector) {
		this.vector = vector;
		this.weight = 1.0;
	}

	void setClassIndex(int classIndex) {
		this.classIndex = classIndex;
	}

	public Value classValue() {
		return valueAt(classIndex);
	}

	public Value classValue(Value val) {
		Value old = valueAt(classIndex);
		setValueAt(classIndex, val);
		return old;
	}

	public Value[] getVector() {
		return vector;
	}

	public double[] getDoubleVector() {
		double[] ret = new double[size()];
		for (int i = 0; i < ret.length; i++) {
			ret[i] = (Double) valueAt(i).getValue();
		}
		return ret;
	}

	public void setVector(Value... vector) {
		this.vector = vector;
	}

	public void setValueAt(int idx, Value v) {
		vector[idx] = v;
	}

	public Value valueAt(int idx) {
		return vector[idx];
	}

	public double doubleValueAt(int idx) {
		return (Double) valueAt(idx).getValue();
	}

	public int size() {
		return vector.length;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

	public Object clone() {
		return new Instance(vector.clone());
	}

	@Override
	public String toString() {
		return Arrays.toString(vector);
	}

	@Override
	public Iterator<Value> iterator() {
		return Arrays.asList(vector).iterator();
	}
}
